package com.ecoolex.ying.space.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.ying.space.core.dao.UserWorksImagesDAO;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksImages;
import com.ecoolex.ying.space.entity.UserWorksImages.Column;
import com.ecoolex.ying.space.pojo.AddUserWorksImageDTO;
import com.ecoolex.ying.space.pojo.AddUserWorksImagesDTO;
import com.ecoolex.ying.space.pojo.EditUserWorksImageDTO;


@Service
public class UserWorksImagesManager {

    @Autowired
    private StorageService storageService;

    @Autowired
    private UserWorksImagesDAO dao;

    @Autowired
    private UserWorksShoppingManager userWorksShoppingManager;

    public UserWorksImages createUserWorksImages(UserWorks userWorks, AddUserWorksImageDTO addUserWorksImageDTO, String createUser) {
        // 获取图片列表
        List<AddUserWorksImagesDTO> addUserWorksImagesDTOList = addUserWorksImageDTO.getAddUserWorksImagesDTOList();
        JSONArray jsonArray = new JSONArray();
        for (AddUserWorksImagesDTO addUserWorksImagesDTO : addUserWorksImagesDTOList) {
            BizAssert.isTrue(storageService.checkSource(new UploadSource("UserWorksImages", "Images"),
                    addUserWorksImagesDTO.getStorageId()), ResultCode.VERIFY.build(10), "图片资源id无效");
            jsonArray.add(addUserWorksImagesDTO.getStorageId());
        }

        UserWorksImages userWorksImages = new UserWorksImages();
        userWorksImages.setContent(addUserWorksImageDTO.getContent());
        userWorksImages.setWorksId(userWorks.getWorksId());
        userWorksImages.setUserId(userWorks.getUserId());
        userWorksImages.setDeleted(UserWorksImages.NOT_DELETED);
        userWorksImages.setCreateUser(createUser);
        userWorksImages.setCreateTime(Date.from(Instant.now()));
        userWorksImages.setUpdateUser(createUser);
        userWorksImages.setUpdateTime(Date.from(Instant.now()));
        userWorksImages.setImages(jsonArray.toString());
        dao.insert(userWorksImages);
        return userWorksImages;
    }

    public void editUserWorksImages(UserWorks userWorks, EditUserWorksImageDTO editUserWorksImageDTO, String updateUser) {
//        UserWorksImages userWorksImages = getByWorksId(editUserWorksImageDTO.getWorksId());
//        BizAssert.isTrue(Check.notNull(userWorksImages), ResultCode.VERIFY.build(10), "图文作品不存在");
//        userWorksImages.setContent(editUserWorksImageDTO.getContent());
//        userWorksImages.setUpdateUser(updateUser);
//        userWorksImages.setUpdateTime(Date.from(Instant.now()));
//        // 获取图片列表
//        List<AddUserWorksImagesDTO> worksImagesDTOList = editUserWorksImageDTO.getAddUserWorksImagesDTOList();
//        JsonArray jsonArray = new JsonArray();
//        for (AddUserWorksImagesDTO userWorksImagesDTO : worksImagesDTOList) {
//            jsonArray.add(userWorksImagesDTO.getStorageId());
//            // 更新购物标签
//            userWorksShoppingManager.updateUserWorksShopping(userWorks,
//                userWorksImagesDTO.getStorageId(),
//                userWorksImagesDTO.getAddUserWorksShoppingsDTOList(),
//                updateUser);
//        }
//        userWorksImages.setImages(jsonArray.toString());
//        dao.updateByPrimaryKeySelective(userWorksImages,
//            Column.excludes(Column.deleted, Column.createUser, Column.createTime));
    }

    public void delete(UserWorks userWorks, String updateUser) {
        UserWorksImages userWorksImages = getByWorksId(userWorks.getWorksId());
        if (Check.notNull(userWorksImages)) {
            userWorksImages.setDeleted(UserWorksImages.IS_DELETED);
            userWorksImages.setUpdateUser(updateUser);
            userWorksImages.setUpdateTime(Date.from(Instant.now()));
            dao.updateByPrimaryKeySelective(userWorksImages, Column.deleted, Column.updateUser, Column.updateTime);
            userWorksShoppingManager.deleteByWorksId(userWorks.getWorksId(), updateUser);
        }
    }

    public UserWorksImages getByWorksId(Integer worksId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(worksId, UserWorksImages.NOT_DELETED);
    }
}